package com.gujiangbo.redemo;

import org.junit.jupiter.api.Test;
import redis.clients.jedis.Jedis;

import java.util.List;
import java.util.Set;

/**
 * Set 集合使用
 */
public class RedisSetDemo {
    private Jedis jedis = JedisFactory.getJedis();

    /**
     * sadd key element [element .....] 集合中添加元素
     */
    @Test
    public void testSadd() {
        Long myset = jedis.sadd("myset", "a", "b", "c");
        String[] msg = new String[100000];
        for (int i = 0; i < 100000; i++) {
            msg[i] = "I Love You " + i;
        }
        Long love = jedis.sadd("love", msg);
        System.out.println("myset 添加元素个数" + myset);
        System.out.println("love 添加元素个数" + love);
        //创建两个集合，后面会进行交集等运算
        jedis.sadd("class:1", "张三", "李四", "王五", "赵六");
        jedis.sadd("class:2", "张三", "李四", "周伯通");
    }

    /**
     * srem key element [element ....] 删除元素
     */
    @Test
    public void testSrem() {
        jedis.srem("myset", "a");
    }

    /**
     * smembers key 获取所有元素
     */
    @Test
    public void testSmembers() {
        Set<String> love = jedis.smembers("love");
        love.parallelStream().forEach(System.out::println);
    }

    /**
     * Scard key 计算元素个数
     */
    @Test
    public void testScard() {
        Long love = jedis.scard("love");
        System.out.println("love:" + love);
    }

    /**
     * Sismember key element 判断元素是否在集合中
     */
    @Test
    public void testSismember() {
        Boolean sismember = jedis.sismember("myset", "b");
        System.out.println(sismember);
    }

    /**
     * srandmember key [count] 随机从集合返回指定个数元素
     */
    @Test
    public void testSrandMember() {
        List<String> love = jedis.srandmember("love", 20);
        love.parallelStream().forEach(System.out::println);
    }

    /**
     * spop key 从集合中随机弹出元素,弹出后的元素将被从集合中删除掉
     */
    @Test
    public void testspop() {
        String love = jedis.spop("love");
        System.out.println(love);
    }

    /*集合操作*/

    /**
     * sinter key [key ....] 求多个集合的交集
     * sinterstore dstkey key1 key2 .. 多个集合交集后添加到新集合dstkey中
     */
    @Test
    public void testSinter() {
        Set<String> sinter = jedis.sinter("class:1", "class:2");
        sinter.parallelStream().forEach(System.out::println);
        jedis.sinterstore("class1_2_sin", "class:1", "class:2");
    }

    /**
     * sunion key [key ...] 多个集合的并集
     * sunionstore dstkey key1 key2 .... 将多个key 并集后添加到新集合dstkey中
     */
    @Test
    public void testSunion() {
        Set<String> sunion = jedis.sunion("class:1", "class:2");
        sunion.forEach(System.out::println);
        jedis.sunionstore("class1_2_sun", "class:1", "class:2");

    }

    /**
     * sdiff key [key ...] 求多个集合的差集
     * sdiffstore destination key [key ...] 将差集结果保存在目标key中
     */
    @Test
    public void testSdiff() {
        System.out.println("class:1 - class:2");
        Set<String> sdiff = jedis.sdiff("class:1", "class:2");
        jedis.sdiffstore("class1_2_diff", "class:1", "class:2");
        sdiff.forEach(System.out::println);
        System.out.println("----------------");
        System.out.println("class:2 - class:1");
        Set<String> sdiff2 = jedis.sdiff("class:2", "class:1");
        jedis.sdiffstore("class2_1_diff", "class:2", "class:1");
        sdiff2.forEach(System.out::println);

    }


}
